package com.kernel.jdbc;

import java.sql.*;

/****************************************
 * @name PreparedStatementDemo
 * @description
 * @author 黄彬恒
 * @since 2018/8/23
 ***************************************/
public class PreparedStatementDemo {
    public static void main(String[] args) {
        // try {
        //     Class<?> clazz = Class.forName(DBDRIVER);// 手动加载驱动程序(检验驱动程序classpath是否成功配置)
        //     System.out.println(clazz.getName());
        // } catch (ClassNotFoundException e) {
        //     e.printStackTrace();
        // }
        Connection conn = null; // 声明数据库连接
        PreparedStatement pstmt = null;// 数据库预处理操作(预防SQL注入攻击)
        String sql = "select id,age,cup_size,money from girl";
        try {
            conn = DriverManager.getConnection(ConnectionDemo01.DBURL, ConnectionDemo01.DBUSER, ConnectionDemo01.DBPASSWORD);// 建立数据库连接
            pstmt = conn.prepareStatement(sql);// 实例化PreparedStatement对象
            ResultSet rs = pstmt.executeQuery();// 执行数据库查询操作
            while (rs.next()) {
                System.out.println(rs.getInt(1));
                System.out.println(rs.getInt(2));
                System.out.println(rs.getString(3));
                System.out.println(rs.getDouble(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
